// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {


    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "showBannerAd", "()V");
        }
        this.node.on(cc.Node.EventType.TOUCH_END, this.checkClose, this);
    }

    onDestroy() {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "hideBannerAd", "()V");
        }
        this.node.off(cc.Node.EventType.TOUCH_END, this.checkClose, this);
    }

    start () {

    }

    // update (dt) {}

    public checkClose(event: cc.Touch) {
        let imBg = this.node.getChildByName('im_bg');
        let touchPos = imBg.convertToNodeSpaceAR(event.getLocation());
        cc.log("touch:"+touchPos);
        if (Math.abs(touchPos.x) > Math.abs(imBg.getContentSize().width/2) ||
                touchPos.y > imBg.getContentSize().height ||
                touchPos.y < 0) {
            this.node.active = false;
            this.node.destroy();
        }
    }

    public clickClose() {
        this.node.active = false;
        this.node.destroy();
    }
}
